//
//  MCLaunchViewController.m
//  LDYL
//
//  Created by menhao on 2018/8/14.
//  Copyright © 2018年 TLYL01. All rights reserved.
//

#import "MCLaunchViewController.h"
#import "MCGetIPAdress.h"

@interface MCLaunchViewController ()<NSURLSessionDelegate>

@property (nonatomic, assign) NSInteger btnTag;

@end

@implementation MCLaunchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    /*
     * launchImgV
     */
    UIImageView *launchImgV = [[UIImageView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:launchImgV];
    NSString *imgName = @"";
    if (IS_iPhone_4S) {
        imgName = @"6401136";
    } else if (IS_iPhone_5) {
        imgName = @"6401136";
    }else if (IS_iPhone_6) {
        imgName = @"7501334";
    }else if (IS_iPhone6_Plus) {
        imgName = @"启动页@3x";
    }else if (IS_iPhoneX) {
        imgName = @"11252436";
    }
    launchImgV.image = [UIImage imageNamed:imgName];
    
    
}

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
//    [self loadFastLineData];
    
}

/*
 * https认证
 */

- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler{
    
    
    SecTrustRef servertrust = challenge.protectionSpace.serverTrust;
    
    SecCertificateRef certi= SecTrustGetCertificateAtIndex(servertrust, 0);
    
    NSData *certidata = CFBridgingRelease(CFBridgingRetain(CFBridgingRelease(SecCertificateCopyData(certi))));
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"https" ofType:@"cer"];
    
    NSData *localCertiData = [NSData dataWithContentsOfFile:path];
    
    if ([certidata isEqualToData:localCertiData]) {
        
        NSURLCredential *credential = [[NSURLCredential alloc] initWithTrust:servertrust];
        
        [challenge.sender useCredential:credential forAuthenticationChallenge:challenge];
        
        completionHandler(NSURLSessionAuthChallengeUseCredential, credential);
        
        NSLog(@"服务端证书认证通过");
        
    }else {
        
        completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil);
        
        NSLog(@"服务端认证失败");
        
    }
    
    
    
}

- (void)loadXianLuWithUrl:(NSString *)urlStrF andXianLutitleTag:(NSInteger )tag secLabel:(UILabel *)secLabel  semaphore:(dispatch_semaphore_t)semaphore{
    NSURL *url=[NSURL URLWithString: urlStrF];
    [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[[NSOperationQueue alloc]init]];
    NSURLSession *session=[NSURLSession sharedSession];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"GET";
    NSString *uuid = [UIDevice currentDevice].identifierForVendor.UUIDString;
    NSString *ip =  [MCGetIPAdress getIPAddress:YES];
    [request addValue:uuid forHTTPHeaderField:@"X-APP-CID"];
    [request addValue:[NSString getAgentInfo] forHTTPHeaderField:@"X-APP-AGENT"];
    [request addValue:@"JAVAAPI" forHTTPHeaderField:@"X-PROTOCOL"];
    [request addValue:ip forHTTPHeaderField:@"X-APP-IP"];
    WS(wself);
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    //设置请求超时为10秒钟
    configuration.timeoutIntervalForRequest = 10;

    NSURLSessionDataTask *dataTask=[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        dispatch_semaphore_signal(semaphore);
     
        NSHTTPURLResponse *re = (NSHTTPURLResponse *)response;
        dispatch_async(dispatch_get_main_queue(), ^{
            if (re.statusCode == 200) {
                    wself.btnTag = tag;
//                    if (wself.btnTag == 1) {
//                        xianLuSeleted  = XianLuYi;
//                    } else if (wself.btnTag == 2) {
//                        xianLuSeleted  = XianLuEr;
//                    }else if (wself.btnTag == 3) {
//                        xianLuSeleted  = XianLuSan;
//                    }else if (wself.btnTag == 4) {
//                        xianLuSeleted  = XianLuSi;
//                    }
                
                [[NSNotificationCenter defaultCenter] postNotificationName:@"LaunchViewControllerHadHiddened" object:nil userInfo:nil];
                if (wself.LaunchViewControllerHadHiddened) {
                    wself.LaunchViewControllerHadHiddened();
                }
                
            }
        });
        
    } ];
    [dataTask resume];
}


// load data
//-(void)loadFastLineData{
//
//    [SVProgressHUD showWithStatus:@"线路优化中..."];
//    WS(wself);
//    //发送请求
////    NSString *urlStrF = [NSString stringWithFormat:@"%@web-api/api/v4/test_line",SEVERBASEURL];
////    NSString *urlStrS = [NSString stringWithFormat:@"%@web-api/api/v4/test_line",SEVERBASEURLER];
////    NSString *urlStrT = [NSString stringWithFormat:@"%@web-api/api/v4/test_line",SEVERBASEURLSAN];
////    NSString *urlStrFour = [NSString stringWithFormat:@"%@web-api/api/v4/test_line",SEVERBASEURLSI];
//    // 创建信号量
//    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
//    // 创建全局并行
//    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//    dispatch_group_t group = dispatch_group_create();
//
//    dispatch_group_async(group, queue, ^{
//        NSLog(@"task1");
//        [wself loadXianLuWithUrl:urlStrF andXianLutitleTag:1 secLabel:nil  semaphore:semaphore];
//    });
//
//    dispatch_group_async(group, queue, ^{
//        NSLog(@"task2");
//        [wself loadXianLuWithUrl:urlStrS andXianLutitleTag:2 secLabel:nil  semaphore:semaphore];
//
//    });
//
//    dispatch_group_async(group, queue, ^{
//        NSLog(@"task3");
//        [wself loadXianLuWithUrl:urlStrT andXianLutitleTag:3 secLabel:nil  semaphore:semaphore];
//    });
//    dispatch_group_async(group, queue, ^{
//        NSLog(@"task4");
//        [wself loadXianLuWithUrl:urlStrFour andXianLutitleTag:4 secLabel:nil semaphore:semaphore];
//    });
//    dispatch_group_notify(group, queue, ^{
//
//        // 三个请求对应三次信号等待
//        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
//        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
//        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
//        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
//        dispatch_async(dispatch_get_main_queue(), ^{
//            NSLog(@"task finish");
//
//
//        });
//    });
//
//}
@end
